package com.controller;

import com.entity.Channel;
import com.service.ChannelServer;
import com.service.impl.ChannelServerImpl;
import com.util.json.Result;
import com.util.sql.PageModel;

import javax.servlet.annotation.WebServlet;

/**
 * 频道接口
 * 增，删，改，查  基本功能
 */
@WebServlet("/Channel")
public class ChannelController {
    private ChannelServer server = new ChannelServerImpl();
    /**
     * 增
     * @return
     */
    public String add(Channel chan){
        try {
            Channel channel = server.add(chan);
            return channel == null ? Result.fial("添加失败") : Result.successObj(channel);
        }catch (Exception e){
            e.printStackTrace();
            return Result.fial(e.getMessage());
        }
    }

    /**
     * 删除
     * @param chan
     * @return
     */
    public String del(Channel chan){
        try {
            int i = server.del(chan);
            return i <= 0 ? Result.fial("删除失败") : Result.success("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fial(e.getMessage());
        }

    }

    /**
     * 改
     * @param chan
     * @return
     */
    public String update(Channel chan){
        try {
            int i  = server.update(chan);
            return i <= 0 ? Result.fial("修改失败") : Result.success("修改成功");
        }catch (Exception e){
            e.printStackTrace();
            return Result.fial(e.getMessage());
        }
    }

    /**
     * 分页查看帖子
     * @param page
     * @param size
     * @return
     */
    public String queryPage(int page,int size){
        try {
            PageModel pageModel = server.queryPage(page,size,new Channel());
            return pageModel == null ? Result.fial("无数据"):Result.successModelObj(pageModel);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fial(e.getMessage());
        }

    }

    /**
     * 单个频道查询  查询该频道下的所有文章
     * @param
     * @return
     */
    public String querySingle(){
        return "";
    }
}
